2. Content
2.1 Question 1
2.1.1 Read Data
##
## Do not execute...
##
## Remove all objects include hidden objects.
rm(list = ls(all.names = TRUE))
## get currency dataset online.
getFX('USD/JPY', from = '2014-01-01', to = '2017-01-20')
USDJPY <- xts(USDJPY[, -1], order.by = USDJPY$Date)
## dateID
dateID <- index(USDJPY)
dateID0 <- ymd('2015-01-01')
dateID <- dateID[dateID > dateID0]
obs.data <- USDJPY[index(USDJPY) > dateID0]
## Now we try to use the daily mean value which is (Hi + Lo) / 2.
pred.data <- ldply(dateID, function(dt) {
smp = USDJPY
dtr = last(index(smp[index(smp) < dt]))
smp = smp[paste0(dtr %m-% years(1), '/', dtr)]
frd = as.numeric(difftime(dt, dtr), units = 'days')
fit = ets(smp) #https://www.otexts.org/fpp/7/7
data.frame(Date = dt, forecast(fit, h = frd)) %>% tbl_df
}, .parallel = FALSE) %>% tbl_df
cmp.data <- xts(pred.data[, -1], order.by = pred.data$Date)
cmp.data <- cbind(cmp.data, obs.data)
rm(obs.data, pred.data)
# Test the models
lm(Point.Forecast~ USD.JPY, data = cmp.data)
MCMCregress(Point.Forecast~ USD.JPY, data = cmp.data)
plot(forecast(fit))
forecast(fit, h = 4)
## Mn(Fund) to retrieve the Mean column.
has.Mn <- function (x, which = FALSE) {
colAttr <- attr(x, 'Mn')
if(!is.null(colAttr))
return(if (which) colAttr else TRUE)
loc <- grep('Mean', colnames(x), ignore.case = TRUE)
if(!identical(loc, integer(0))) {
return(if (which) loc else TRUE)
} else FALSE
}
Mn <- function(x) {
if(has.Mn(x))
return(x[, grep("Mean", colnames(x), ignore.case = TRUE)])
stop("subscript out of bounds: no column name containing \"Mean\"")
}
## get currency dataset online.
## http://stackoverflow.com/questions/24219694/get-symbols-quantmod-ohlc-currency-data
#'@ getFX('USD/JPY', from = '2014-01-01', to = '2017-01-20')
## getFX() doesn't shows Op, Hi, Lo, Cl price but only price. Therefore no idea to place bets.
#'@ USDJPY <- getSymbols('JPY=X', src = 'yahoo', from = '2014-01-01',
#'@ to = '2017-01-20', auto.assign = FALSE)
#'@ names(USDJPY) <- str_replace_all(names(USDJPY), 'JPY=X', 'USDJPY')
#'@ USDJPY <- xts(USDJPY[, -1], order.by = USDJPY$Date)
#'@ saveRDS(USDJPY, './data/USDJPY.rds')
USDJPY <- read_rds(path = './data/USDJPY.rds')
2.1.2 ARIMA vs ETS
Below are some articles with regards exponential smoothing.
- Recent Advances in Robust Statistics: Theory and Applications
- Error, trend, seasonality - ets and its forecast model friends
- A study of outliers in the exponential smoothing approach to forecasting
- 8.10 ARIMA vs ETS
It is a common myth that ARIMA models are more general than exponential smoothing. While linear exponential smoothing models are all special cases of ARIMA models, the non-linear exponential smoothing models have no equivalent ARIMA counterparts. There are also many ARIMA models that have no exponential smoothing counterparts. In particular, every ETS model2 ets() : Usually a three-character string identifying method using the framework terminology of Hyndman et al. (2002) and Hyndman et al. (2008). The first letter denotes the error type (“A”, “M” or “Z”); the second letter denotes the trend type (“N”,“A”,“M” or “Z”); and the third letter denotes the season type (“N”,“A”,“M” or “Z”). In all cases, “N”=none, “A”=additive, “M”=multiplicative and “Z”=automatically selected. So, for example, “ANN” is simple exponential smoothing with additive errors, “MAM” is multiplicative Holt-Winters’ method with multiplicative errors, and so on. It is also possible for the model to be of class “ets”, and equal to the output from a previous call to ets. In this case, the same model is fitted to y without re-estimating any smoothing parameters. See also the use.initial.values argument. is non-stationary, while ARIMA models can be stationary.
The ETS models with seasonality or non-damped trend or both have two unit roots (i.e., they need two levels of differencing to make them stationary). All other ETS models have one unit root (they need one level of differencing to make them stationary).
The following table gives some equivalence relationships for the two classes of models.
| ETS model | ARIMA model | Parameters |
|---|---|---|
| \(ETS(A, N, N)\) | \(ARIMA(0, 1, 1)\) | \(θ_{1} = α − 1\) |
| \(ETS(A, A, N)\) | \(ARIMA(0, 2, 2)\) | \(θ_{1} = α + β − 2\) |
| \(θ_{2} = 1 − α\) | ||
| \(ETS(A, A_{d}, N)\) | \(ARIMA(1, 1, 2)\) | \(ϕ_{1} = ϕ\) |
| \(θ_{1} = α + ϕβ − 1 − ϕ\) | ||
| \(θ_{2} = (1 − α)ϕ\) | ||
| \(ETS(A, N, A)\) | \(ARIMA(0, 0, m)(0, 1, 0)_{m}\) | |
| \(ETS(A, A, A)\) | \(ARIMA(0, 1, m+1)(0, 1, 0)_{m}\) | |
| \(ETS(A, A_{d}, A)\) | \(ARIMA(1, 0, m+1)(0, 1, 0)_{m}\) |
For the seasonal models, there are a large number of restrictions on the ARIMA parameters.
Kindly refer to 8.10 ARIMA vs ETS for further details.
## Now we try to use the daily mean value which is (Hi + Lo) / 2.
## Hi for predict daily highest price. (selling daytrade)
## Lo for predict daily lowest price. (buying daytrade)
simPrice <- function(mbase, .prCat = 'Mn', .baseDate = ymd('2015-01-01'), .parallel = FALSE, .model = 'ZZZ') {
if(!is.xts(mbase)) mbase <- xts(mbase[, -1], order.by = mbase$Date)
## dateID
dateID <- index(mbase)
if(!is.Date(.baseDate)) {
dateID0 <- ymd(.baseDate); rm(.baseDate)
} else {
dateID0 <- .baseDate; rm(.baseDate)
}
dateID <- dateID[dateID >= dateID0]
## Set as our daily settlement price.
obs.data <- mbase[index(mbase) > dateID0]
price.category <- c('Op', 'Hi', 'Mn', 'Lo', 'Cl')
if(.prCat %in% price.category) {
if(.prCat == 'Op') {
obs.data2 <- Op(mbase)
} else if(.prCat == 'Hi') {
obs.data2 <- Hi(mbase)
} else if(.prCat == 'Mn') { #mean of highest and lowest
obs.data2 <- cbind(Hi(mbase), Lo(mbase), USDJPY.Md = rowMeans(cbind(Hi(mbase), Lo(mbase))))[,-c(1:2)]
} else if(.prCat == 'Lo') {
obs.data2 <- Lo(mbase)
} else if(.prCat == 'Cl') {
obs.data2 <- Cl(mbase)
} else {
stop('Kindly choose .prCat = "Op", .prCat = "Hi", .prCat = "Mn", .prCat = "Lo" or .prCat = "Cl".')
}
} else {
stop('Kindly choose .prCat = "Op", .prCat = "Hi", .prCat = "Mn", .prCat = "Lo" or .prCat = "Cl".')
}
pred.data <- ldply(dateID, function(dt) {
smp = obs.data2
dtr = last(index(smp[index(smp) < dt]))
smp = smp[paste0(dtr %m-% years(1), '/', dtr)]
frd = as.numeric(difftime(dt, dtr), units = 'days')
fit = ets(smp, model = .model) #exponential smoothing model.
if(frd > 1) dt = seq(dt - days(frd), dt, by = 'days')[-1]
data.frame(Date = dt, forecast(fit, h = frd)) %>% tbl_df
}, .parallel = .parallel) %>% tbl_df
cmp.data <- xts(pred.data[, -1], order.by = pred.data$Date)
cmp.data <- cbind(cmp.data, obs.data)
rm(obs.data, pred.data)
return(cmp.data)
}
## Sorry ARIMA, but I’m Going Bayesian
## http://multithreaded.stitchfix.com/blog/2016/04/21/forget-arima/
#'@ library('bsts')
## Need to testing and compare the models (packages : MCMCPack and bsts).
## Modelling
fit.op <- simPrice(USDJPY, .prCat = 'Op') #will take a minute
fit.hi <- simPrice(USDJPY, .prCat = 'Hi') #will take a minute
fit.mn <- simPrice(USDJPY, .prCat = 'Mn') #will take a minute
fit.lo <- simPrice(USDJPY, .prCat = 'Lo') #will take a minute
fit.cl <- simPrice(USDJPY, .prCat = 'Cl') #will take a minute
2.1.3 MCMC
Need to refer to MCMC since I am using exponential smoothing models…
## Need to test and read through the MCMCregress... after few months later (when free)... Start working as a servant at Bah-Kut-Teh restorant tommorrow 01-Mar-2017.
## Test the models
## opened price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fit.op))
##
## Call:
## lm(formula = Point.Forecast ~ USDJPY.Close, data = fit.op)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.4353 -0.4004 -0.0269 0.3998 3.3978
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.180332 0.490019 0.368 0.713
## USDJPY.Close 0.998722 0.004256 234.666 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7286 on 533 degrees of freedom
## (216 observations deleted due to missingness)
## Multiple R-squared: 0.9904, Adjusted R-squared: 0.9904
## F-statistic: 5.507e+04 on 1 and 533 DF, p-value: < 2.2e-16
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fit.op))
##
## Iterations = 1001:11000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## (Intercept) 0.1808 0.489606 4.896e-03 4.896e-03
## USDJPY.Close 0.9987 0.004257 4.257e-05 4.257e-05
## sigma2 0.5330 0.033014 3.301e-04 3.301e-04
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## (Intercept) -0.7795 -0.1487 0.1848 0.5094 1.1441
## USDJPY.Close 0.9904 0.9959 0.9987 1.0016 1.0070
## sigma2 0.4716 0.5100 0.5317 0.5549 0.6009
## highest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fit.hi))
##
## Call:
## lm(formula = Point.Forecast ~ USDJPY.Close, data = fit.hi)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.3422 -0.3298 -0.0987 0.2166 3.2868
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.140616 0.379253 3.008 0.00276 **
## USDJPY.Close 0.993982 0.003294 301.765 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5639 on 533 degrees of freedom
## (216 observations deleted due to missingness)
## Multiple R-squared: 0.9942, Adjusted R-squared: 0.9942
## F-statistic: 9.106e+04 on 1 and 533 DF, p-value: < 2.2e-16
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fit.hi))
##
## Iterations = 1001:11000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## (Intercept) 1.1410 0.378933 3.789e-03 3.789e-03
## USDJPY.Close 0.9940 0.003295 3.295e-05 3.295e-05
## sigma2 0.3193 0.019776 1.978e-04 1.978e-04
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## (Intercept) 0.3978 0.8860 1.1441 1.3953 1.8865
## USDJPY.Close 0.9875 0.9918 0.9939 0.9962 1.0004
## sigma2 0.2825 0.3055 0.3185 0.3324 0.3599
## mean price fit data (mean price of daily highest and lowest price)
summary(lm(Point.Forecast~ USDJPY.Close, data = fit.mn))
##
## Call:
## lm(formula = Point.Forecast ~ USDJPY.Close, data = fit.mn)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.55047 -0.26416 -0.00996 0.26743 1.81654
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.106616 0.326718 0.326 0.744
## USDJPY.Close 0.999098 0.002838 352.091 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4858 on 533 degrees of freedom
## (216 observations deleted due to missingness)
## Multiple R-squared: 0.9957, Adjusted R-squared: 0.9957
## F-statistic: 1.24e+05 on 1 and 533 DF, p-value: < 2.2e-16
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fit.mn))
##
## Iterations = 1001:11000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## (Intercept) 0.1069 0.326443 3.264e-03 3.264e-03
## USDJPY.Close 0.9991 0.002838 2.838e-05 2.838e-05
## sigma2 0.2369 0.014676 1.468e-04 1.468e-04
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## (Intercept) -0.5333 -0.1127 0.1096 0.3260 0.7492
## USDJPY.Close 0.9935 0.9972 0.9991 1.0010 1.0046
## sigma2 0.2096 0.2267 0.2364 0.2467 0.2671
## lowest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fit.lo))
##
## Call:
## lm(formula = Point.Forecast ~ USDJPY.Close, data = fit.lo)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1318 -0.2450 0.0860 0.3331 1.4818
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.3885 0.3684 -3.769 0.000182 ***
## USDJPY.Close 1.0083 0.0032 315.094 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5478 on 533 degrees of freedom
## (216 observations deleted due to missingness)
## Multiple R-squared: 0.9947, Adjusted R-squared: 0.9947
## F-statistic: 9.928e+04 on 1 and 533 DF, p-value: < 2.2e-16
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fit.lo))
##
## Iterations = 1001:11000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## (Intercept) -1.3881 0.368114 3.681e-03 3.681e-03
## USDJPY.Close 1.0082 0.003201 3.201e-05 3.201e-05
## sigma2 0.3013 0.018663 1.866e-04 1.866e-04
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## (Intercept) -2.1101 -1.6358 -1.3851 -1.1410 -0.6638
## USDJPY.Close 1.0020 1.0061 1.0082 1.0104 1.0145
## sigma2 0.2666 0.2883 0.3006 0.3137 0.3397
## closed price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fit.cl))
##
## Call:
## lm(formula = Point.Forecast ~ USDJPY.Close, data = fit.cl)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.4339 -0.4026 -0.0249 0.3998 3.4032
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.17826 0.49050 0.363 0.716
## USDJPY.Close 0.99873 0.00426 234.437 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7293 on 533 degrees of freedom
## (216 observations deleted due to missingness)
## Multiple R-squared: 0.9904, Adjusted R-squared: 0.9904
## F-statistic: 5.496e+04 on 1 and 533 DF, p-value: < 2.2e-16
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fit.cl))
##
## Iterations = 1001:11000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## (Intercept) 0.1787 0.490086 4.901e-03 4.901e-03
## USDJPY.Close 0.9987 0.004261 4.261e-05 4.261e-05
## sigma2 0.5340 0.033079 3.308e-04 3.308e-04
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## (Intercept) -0.7825 -0.1511 0.1827 0.5077 1.1430
## USDJPY.Close 0.9904 0.9959 0.9987 1.0016 1.0071
## sigma2 0.4725 0.5110 0.5327 0.5559 0.6021
Mean Squared Error
fcdata <- do.call(cbind, list(USDJPY.FPOP.Open = fit.op$Point.Forecast,
USDJPY.FPHI.High = fit.hi$Point.Forecast,
USDJPY.FPMN.Mean = fit.mn$Point.Forecast,
USDJPY.FPLO.Low = fit.lo$Point.Forecast,
USDJPY.FPCL.Close = fit.cl$Point.Forecast,
USDJPY.Open = fit.op$USDJPY.Open,
USDJPY.High = fit.op$USDJPY.High,
USDJPY.Low = fit.op$USDJPY.Low,
USDJPY.Close = fit.op$USDJPY.Close))
fcdata <- na.omit(fcdata)
names(fcdata) <- c('USDJPY.FPOP.Open', 'USDJPY.FPHI.High', 'USDJPY.FPMN.Mean',
'USDJPY.FPLO.Low', 'USDJPY.FPCL.Close', 'USDJPY.Open',
'USDJPY.High', 'USDJPY.Low', 'USDJPY.Close')
## Mean Squared Error : comparison of accuracy
paste('Open = ', mean((fcdata$USDJPY.FPOP.Open - fcdata$USDJPY.Open)^2))
## [1] "Open = 0.524327826450961"
paste('High = ', mean((fcdata$USDJPY.FPHI.High - fcdata$USDJPY.High)^2))
## [1] "High = 0.458369038353778"
paste('Mean = ', mean((fcdata$USDJPY.FPMN.Mean - (fcdata$USDJPY.High + fcdata$USDJPY.Low)/2)^2))
## [1] "Mean = 0.414913471187317"
paste('Low = ', mean((fcdata$USDJPY.FPLO.Low - fcdata$USDJPY.Low)^2))
## [1] "Low = 0.623518861674962"
paste('Close = ', mean((fcdata$USDJPY.FPCL.Close - fcdata$USDJPY.Close)^2))
## [1] "Close = 0.531069865476858"
2.1.4 Data Visualization
Plot graph.
## Plot the models
## opened price fit data
autoplot(forecast(ets(fit.op$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color='#ffcccc', show.legend=FALSE) + labs(x = 'Day', y = 'Forex Price', "Forecasts from ETS model")
#'@ ggplot(data = pd, aes(x = date, y = observed)) + geom_line(color = 'red') + geom_line(aes(y = fitted), color = "blue") + geom_line(aes(y = forecast)) + geom_ribbon(aes(ymin = lo95, ymax = hi95), alpha = .25) + scale_x_date(name = "Time in Decades") + scale_y_continuous(name = "GDP per capita (current US$)") + theme(axis.text.x = element_text(size = 10), legend.justification=c(0,1), legend.position=c(0,1)) + ggtitle("Arima(0,1,1) Fit and Forecast of GDP per capita for Brazil (1960-2013)") + scale_color_manual(values = c("Blue", "Red"), breaks = c("Fitted", "Data", "Forecast")) + ggsave((filename = "gdp_forecast_ggplot.pdf"), width=330, height=180, units=c("mm"), dpi = 300, limitsize = TRUE)
## highest price fit data
autoplot(forecast(ets(fit.hi$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', 'Forecasts from ETS model')
## mean price fit data (mean price of daily highest and lowest price)
autoplot(forecast(ets(fit.mn$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', 'Forecasts from ETS model')
## lowest price fit data
autoplot(forecast(ets(fit.lo$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', 'Forecasts from ETS model')
## opened price fit data
autoplot(forecast(ets(fit.cl$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', 'Forecasts from ETS model')
source('./function/plotChart2.R', local = TRUE)
suppressAll(rm(fit.op, fit.hi, fit.mn, fit.lo, fit.cl))
plotChart2(fcdata, initialName = 'FP', chart.type = 'FP', graph.title = 'USDJPY')
2.1.5 Staking Model
Staking function. Here I apply Kelly criterion as the betting strategy. I don’t pretend to know the order of price flutuation flow from the Hi-Lo price range, therefore I just using Closing price for settlement while the staking price restricted within the variance (Hi-Lo) to made the transaction stand. The settled price can only be closing price unless staking price is opening price which sellable within the Hi-Lo range.
Due to we cannot know the forecasted sell/buy price and also forecasted closing price which is coming first solely from Hi-Lo data, therefore the Profit&Loss will slidely different (sell/buy price = forecasted sell/buy price).
- Forecasted profit = edge based on forecasted sell/buy price - forecasted settled price.
- If the forecasted sell/buy price doesn’t exist within the Hi-Lo price, then the transaction is not stand.
- If the forecasted settled price does not exist within the Hi-Lo price, then the settled price will be the real closing price.
simStakes <- function(mbase, .prCat = 'Op', .baseDate = ymd('2015-01-01'), .parallel = FALSE, .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = FALSE, .filterBets = FALSE) {
## .setPrice need to set by refer to closing price, otherwise the P%L will be wrong due to we unable
## know the price flow based on Hi-Lo price.
## Here I set .setPrice to options as : .setPrice = 'Op', .setPrice = 'Hi', .setPrice = 'Mn', .setPrice = 'Lo', .setPrice = 'Cl', .setPrice = 'FPOP', .setPrice = 'FPHI', .setPrice = 'FPMN', .setPrice = 'FPLO', .setPrice = 'FPCL'.
## Kindly set .initialFundSize = 1000 but not .initialFundSize = log(1000) for risk management, .fundLeverageLog = FALSE just do not exp() the log() fund size.
#'@ source('./function/simPrice.R', local = TRUE)
if(!is.numeric(.initialFundSize) & length(.initialFundSize) != 1 & .initialFundSize <= 0) {
stop('Kindly insert a numeric number as initial fund size.')
}
if(.fundLeverageLog == TRUE) .initialFundSize = log(.initialFundSize)
.setPriceList <- c('Op', 'Hi', 'Mn', 'Lo', 'Cl', 'FPOP', 'FPHI', 'FPMN', 'FPLO', 'FPCL')
if(.setPrice %in% .setPriceList) {
.setPrice <- .setPrice
} else {
stop("Kindly set .setPrice among c('Op', 'Hi', 'Mn', 'Lo', 'Cl', 'FPOP', 'FPHI', 'FPMN', 'FPLO', 'FPCL')")
}
nm <- str_extract_all(names(mbase), '^(.*?)+\\.') %>% unlist %>% unique
names(mbase) <- str_replace_all(names(mbase), '^(.*?)+\\.', 'USDJPY.')
## forecast staking price.
fit1 <- simPrice(mbase, .prCat = .prCat, .baseDate = .baseDate, .parallel = .parallel)
fit1 <- data.frame(Date = index(fit1), coredata(fit1)) %>% tbl_df
fit1 <- na.omit(fit1)
## forecast settlement price.
fit2 <- simPrice(mbase, .prCat = .setPrice, .baseDate = .baseDate, .parallel = .parallel)
fit2 <- data.frame(Date = index(fit2), coredata(fit2)) %>% tbl_df
fit2 <- na.omit(fit2)
## merge dataset
fitm <- cbind(fit1, forClose = fit2$Point.Forecast) %>% tbl_df
## convert to probability.
fitm %<>% mutate(ProbB = pnorm(Point.Forecast, mean = forClose, sd = sd(forClose)), ProbS = 1 - ProbB) #ProbS = pnorm(Point.Forecast, mean = forClose, sd = sd(forClose), lower.tail = FALSE)
## staking model and bankroll management.
## need to refer to Niko Martinen's fund management formula to maximise the stakes and profit base on Kelly models.
## https://github.com/scibrokes/betting-strategy-and-model-validation/blob/master/references/Creating%20a%20Profitable%20Betting%20Strategy%20for%20Football%20by%20Using%20Statistical%20Modelling.pdf
#.... dynamic staking model need to adjusted based on updated bankroll but not portion of fixed USD100 per bet.
fitm %<>% mutate(BR = .initialFundSize) %>%
#'@ mutate(Return.Back = ifelse(Prob > 0.5, Diff * Back * stakes, 0),
#'@ Return.Lay = ifelse(Prob < 0.5, -Diff * Lay * stakes, 0))
mutate(fB = 2 * ProbB - 1, fS = 2 * ProbS - 1,
EUB = ProbB * log(BR * (1 + fB)) + (1 - ProbB) * log(BR * (1 - fB)),
EUS = ProbS * log(BR * (1 + fS)) + (1 - ProbS) * log(BR * (1 - fS)),
#'@ Edge = ifelse(f > 0, EUB, EUS), #For f > 0 need to buy and f <= 0 need to sell.
#need to study on the risk management on "predicted profit" and "real profit".
Edge = ifelse(fB > 0, EUB, ifelse(fS > 0, EUS, 0)),
PF = ifelse(Point.Forecast >= USDJPY.Low &
Point.Forecast <= USDJPY.High,
Point.Forecast, 0), #if forecasted place-bet price doesn't existing within Hi-Lo price, then the buying action is not stand. Assume there has no web bandwith delay.
FC = ifelse(forClose >= USDJPY.Low & forClose <= USDJPY.High,
forClose, USDJPY.Close), #if forecasted settle price doesn't existing within Hi-Lo price, then the closing action at closing price. Assume there has no web bandwith delay.
#'@ Diff = round(forClose - USDJPY.Close, 2),
##forecasted closed price minus real close price.
Buy = ifelse(PF > 0 & FC > PF, 1, 0), ##buy action
Sell = ifelse(PF > 0 & FC < PF, 1, 0), ##sell action
BuyS = Edge * Buy * (forClose - PF),
SellS = Edge * Sell * (PF - forClose),
Profit = BuyS + SellS, Bal = BR + Profit)
#'@ fitm %>% dplyr::select(Point.Forecast, forClose, Prob, BR, f, EU, Edge, PF, FC, Buy, Sell, SP, Bal)
#'@ fitm %>% dplyr::select(ProbB, ProbS, BR, fB, fS, EUB, EUS, Edge, PF, USDJPY.Open, FC, Buy, Sell, BuyS, SellS, Profit, Bal) %>% filter(PF > 0, FC > 0)
for(i in seq(2, nrow(fitm))) {
fitm$BR[i] = fitm$Bal[i - 1]
fitm$fB[i] = 2 * fitm$ProbB[i] - 1
fitm$fS[i] = 2 * fitm$ProbS[i] - 1
fitm$EUB[i] = fitm$ProbB[i] * log(fitm$BR[i] * (1 + fitm$fB[i])) +
(1 - fitm$ProbB[i]) * log(fitm$BR[i] * (1 - fitm$fB[i]))
fitm$EUS[i] = fitm$ProbS[i] * log(fitm$BR[i] * (1 + fitm$fS[i])) +
(1 - fitm$ProbS[i]) * log(fitm$BR[i] * (1 - fitm$fS[i]))
fitm$Edge[i] = ifelse(fitm$fB[i] > 0, fitm$EUB[i],
ifelse(fitm$fS[i] > 0, fitm$EUS[i], 0)) #For f > 0 need to buy and f <= 0 need to sell.
#need to study on the risk management on "predicted profit" and "real profit".
fitm$BuyS[i] = fitm$Edge[i] * fitm$Buy[i] * (fitm$forClose[i] - fitm$PF[i])
fitm$SellS[i] = fitm$Edge[i] * fitm$Sell[i] * (fitm$PF[i] - fitm$forClose[i])
fitm$Profit[i] = fitm$BuyS[i] + fitm$SellS[i]
fitm$Bal[i] = fitm$BR[i] + fitm$Profit[i]
}; rm(i)
names(mbase) <- str_replace_all(names(mbase), '^(.*?)+\\.', nm)
if(.filterBets == TRUE) {
fitm %<>% filter(PF > 0, FC > 0)
}
fitm %<>% mutate(RR = Bal/BR)
## convert the log leverage value of fund size and profit into normal digital figure with exp().
if(.fundLeverageLog == TRUE) fitm %<>% mutate(BR = exp(BR), BuyS = exp(BuyS), SellS = exp(SellS), Profit = exp(Profit), Bal = exp(Profit))
return(fitm)
}
##============================ EVAL = FALSE ================================
##
## Model 1 without leverage.
##
## Placed orders - Fund size with log
mbase = USDJPY
fundOPHI <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = 1000)
fundHIHI <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = 1000)
fundMNHI <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = 1000)
fundLOHI <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = 1000)
fundCLHI <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = 1000)
fundOPMN <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = 1000)
fundHIMN <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = 1000)
fundMNMN <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = 1000)
fundLOMN <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = 1000)
fundCLMN <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = 1000)
fundOPLO <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = 1000)
fundHILO <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = 1000)
fundMNLO <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = 1000)
fundLOLO <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = 1000)
fundCLLO <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = 1000)
fundOPCL <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = 1000)
fundHICL <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = 1000)
fundMNCL <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = 1000)
fundLOCL <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = 1000)
fundCLCL <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = 1000)
## Placed orders - Fund size without log
#'@ fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
#'@ fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
#'@ fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
#'@ fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
#'@
#'@ ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
## A tibble: 20 × 5
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
#1 fundOPHI 2015-01-02 2017-01-20 1000 326.83685 1326.837 1.326837
#2 fundHIHI 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#3 fundMNHI 2015-01-02 2017-01-20 1000 152.30210 1152.302 1.152302
#4 fundLOHI 2015-01-02 2017-01-20 1000 816.63808 1816.638 1.816638
#5 fundCLHI 2015-01-02 2017-01-20 1000 323.18564 1323.186 1.323186
#6 fundOPMN 2015-01-02 2017-01-20 1000 246.68001 1246.680 1.246680
#7 fundHIMN 2015-01-02 2017-01-20 1000 384.90915 1384.909 1.384909
#8 fundMNMN 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#9 fundLOMN 2015-01-02 2017-01-20 1000 529.34170 1529.342 1.529342
#10 fundCLMN 2015-01-02 2017-01-20 1000 221.03926 1221.039 1.221039
#11 fundOPLO 2015-01-02 2017-01-20 1000 268.31155 1268.312 1.268312
#12 fundHILO 2015-01-02 2017-01-20 1000 649.35074 1649.351 1.649351
#13 fundMNLO 2015-01-02 2017-01-20 1000 298.28509 1298.285 1.298285
#14 fundLOLO 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#15 fundCLLO 2015-01-02 2017-01-20 1000 208.85690 1208.857 1.208857
#16 fundOPCL 2015-01-02 2017-01-20 1000 30.55969 1030.560 1.030560
#17 fundHICL 2015-01-02 2017-01-20 1000 400.59057 1400.591 1.400591
#18 fundMNCL 2015-01-02 2017-01-20 1000 117.96808 1117.968 1.117968
#19 fundLOCL 2015-01-02 2017-01-20 1000 530.68975 1530.690 1.530690
#20 fundCLCL 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
##============================ EVAL = FALSE ================================
##
## Leveraged model 2
##
## Placed orders - Fund size with log
fundOPHI <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = log(1000))
fundHIHI <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = log(1000))
fundMNHI <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = log(1000))
fundLOHI <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = log(1000))
fundCLHI <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = log(1000))
fundOPMN <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = log(1000))
fundHIMN <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = log(1000))
fundMNMN <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = log(1000))
fundLOMN <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = log(1000))
fundCLMN <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = log(1000))
fundOPLO <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = log(1000))
fundHILO <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = log(1000))
fundMNLO <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = log(1000))
fundLOLO <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = log(1000))
fundCLLO <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = log(1000))
fundOPCL <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = log(1000))
fundHICL <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = log(1000))
fundMNCL <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = log(1000))
fundLOCL <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = log(1000))
fundCLCL <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = log(1000))
## Placed orders - Fund size with log
#'@ fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
#'@ fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
#'@ fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
#'@ fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
#'@
#'@ ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
## A tibble: 20 × 7
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
#1 fundOPHI 2015-01-02 2017-01-20 6.907755 201.699419 194.79166 29.198982
#2 fundHIHI 2015-01-02 2017-01-20 6.907755 6.907755 0.00000 1.000000
#3 fundMNHI 2015-01-02 2017-01-20 6.907755 75.593102 68.68535 10.943222
#4 fundLOHI 2015-01-02 2017-01-20 6.907755 592.614380 585.70662 85.789718
#5 fundCLHI 2015-01-02 2017-01-20 6.907755 199.023237 192.11548 28.811565
#6 fundOPMN 2015-01-02 2017-01-20 6.907755 145.334081 138.42633 21.039263
#7 fundHIMN 2015-01-02 2017-01-20 6.907755 245.812470 238.90472 35.585000
#8 fundMNMN 2015-01-02 2017-01-20 6.907755 6.907755 0.00000 1.000000
#9 fundLOMN 2015-01-02 2017-01-20 6.907755 359.728088 352.82033 52.075975
#10 fundCLMN 2015-01-02 2017-01-20 6.907755 127.528193 120.62044 18.461597
#11 fundOPLO 2015-01-02 2017-01-20 6.907755 159.124291 152.21654 23.035600
#12 fundHILO 2015-01-02 2017-01-20 6.907755 452.725480 445.81772 65.538726
#13 fundMNLO 2015-01-02 2017-01-20 6.907755 180.580704 173.67295 26.141734
#14 fundLOLO 2015-01-02 2017-01-20 6.907755 6.907755 0.00000 1.000000
#15 fundCLLO 2015-01-02 2017-01-20 6.907755 117.219609 110.31185 16.969276
#16 fundOPCL 2015-01-02 2017-01-20 6.907755 17.669553 10.76180 2.557930
#17 fundHICL 2015-01-02 2017-01-20 6.907755 256.110890 249.20313 37.075849
#18 fundMNCL 2015-01-02 2017-01-20 6.907755 57.745913 50.83816 8.359577
#19 fundLOCL 2015-01-02 2017-01-20 6.907755 357.560612 350.65286 51.762200
#20 fundCLCL 2015-01-02 2017-01-20 6.907755 6.907755 0.00000 1.000000
##============================ EVAL = FALSE ================================
##
## Leveraged model 3
##
## Due to the log(.initialfundSize) generates extremely high return compare to normal figure, I added a new parameter ".fundLeverageLog" which convert the normal fund size value into log into calculation and finally convert back to normal fund size figure."
## Placed orders - Fund size without log but exp() Leveraged.
#'@ fundOPHI <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundHIHI <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundMNHI <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundLOHI <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundCLHI <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundOPMN <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundHIMN <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundMNMN <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundLOMN <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundCLMN <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundOPLO <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundHILO <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundMNLO <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundLOLO <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundCLLO <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundOPCL <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundHICL <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundMNCL <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundLOCL <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = TRUE)
#'@ fundCLCL <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = 1000, .fundLeverageLog = TRUE)
## A tibble: 20 × 7
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
# 1 fundOPHI 2015-01-02 2017-01-20 1000 2.096972e+27 7.701648e+03 2.096972e+24
# 2 fundHIHI 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 3 fundMNHI 2015-01-02 2017-01-20 1000 3.900327e+04 1.064366e+03 3.900327e+01
# 4 fundLOHI 2015-01-02 2017-01-20 1000 2.718282e+00 9.620574e+09 2.718282e-03
# 5 fundCLHI 2015-01-02 2017-01-20 1000 6.689430e+23 6.611671e+03 6.689430e+20
# 6 fundOPMN 2015-01-02 2017-01-20 1000 2.454335e+01 1.027528e+03 2.454335e-02
# 7 fundHIMN 2015-01-02 2017-01-20 1000 2.718282e+00 2.628152e+03 2.718282e-03
# 8 fundMNMN 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 9 fundLOMN 2015-01-02 2017-01-20 1000 2.718282e+00 3.916608e+04 2.718282e-03
# 10 fundCLMN 2015-01-02 2017-01-20 1000 1.585667e+01 9.900735e+02 1.585667e-02
# 11 fundOPLO 2015-01-02 2017-01-20 1000 1.152381e+00 3.455387e+03 1.152381e-03
# 12 fundHILO 2015-01-02 2017-01-20 1000 2.718282e+00 1.541190e+06 2.718282e-03
# 13 fundMNLO 2015-01-02 2017-01-20 1000 1.040560e+00 3.434093e+03 1.040560e-03
# 14 fundLOLO 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 15 fundCLLO 2015-01-02 2017-01-20 1000 1.152544e+00 2.646148e+03 1.152544e-03
# 16 fundOPCL 2015-01-02 2017-01-20 1000 2.919029e+00 5.465890e+02 2.919029e-03
# 17 fundHICL 2015-01-02 2017-01-20 1000 2.718282e+00 2.278748e+05 2.718282e-03
# 18 fundMNCL 2015-01-02 2017-01-20 1000 1.034658e+01 9.680842e+02 1.034658e-02
# 19 fundLOCL 2015-01-02 2017-01-20 1000 2.718282e+00 2.407137e+08 2.718282e-03
# 20 fundCLCL 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
##============================ EVAL = FALSE ================================
##
## Leveraged model 4
##
## Placed orders - Fund size with log and revert back exp() after calculation.
fundOPHI <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundHIHI <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundMNHI <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundLOHI <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundCLHI <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundOPMN <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundHIMN <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundMNMN <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundLOMN <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundCLMN <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundOPLO <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundHILO <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundMNLO <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundLOLO <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundCLLO <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundOPCL <- simStakes(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundHICL <- simStakes(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundMNCL <- simStakes(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundLOCL <- simStakes(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
fundCLCL <- simStakes(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = log(1000), .fundLeverageLog = TRUE)
## Placed orders - Fund size with log
#'@ fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
#'@ fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
#'@ fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
#'@ fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
#'@
#'@ ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
# ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
# A tibble: 20 × 7
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
# 1 fundOPHI 2015-01-02 2017-01-20 6.907755 7.010568e+24 6.130503e+03 1.014884e+24
# 2 fundHIHI 2015-01-02 2017-01-20 6.907755 2.718282e+00 5.350000e+02 3.935116e-01
# 3 fundMNHI 2015-01-02 2017-01-20 6.907755 2.711146e+00 5.344836e+02 3.924786e-01
# 4 fundLOHI 2015-01-02 2017-01-20 6.907755 2.718282e+00 6.039260e+09 3.935116e-01
# 5 fundCLHI 2015-01-02 2017-01-20 6.907755 5.090087e+21 5.261994e+03 7.368655e+20
# 6 fundOPMN 2015-01-02 2017-01-20 6.907755 2.198185e+01 9.372727e+02 3.182199e+00
# 7 fundHIMN 2015-01-02 2017-01-20 6.907755 2.718282e+00 2.362245e+03 3.935116e-01
# 8 fundMNMN 2015-01-02 2017-01-20 6.907755 2.718282e+00 5.350000e+02 3.935116e-01
# 9 fundLOMN 2015-01-02 2017-01-20 6.907755 2.718282e+00 3.257372e+04 3.935116e-01
# 10 fundCLMN 2015-01-02 2017-01-20 6.907755 1.441047e+01 9.002950e+02 2.086129e+00
# 11 fundOPLO 2015-01-02 2017-01-20 6.907755 1.161927e+00 2.719083e+03 1.682061e-01
# 12 fundHILO 2015-01-02 2017-01-20 6.907755 2.718282e+00 1.202578e+06 3.935116e-01
# 13 fundMNLO 2015-01-02 2017-01-20 6.907755 1.043987e+00 2.932865e+03 1.511326e-01
# 14 fundLOLO 2015-01-02 2017-01-20 6.907755 2.718282e+00 5.350000e+02 3.935116e-01
# 15 fundCLLO 2015-01-02 2017-01-20 6.907755 1.166220e+00 1.999664e+03 1.688276e-01
# 16 fundOPCL 2015-01-02 2017-01-20 6.907755 2.861825e+00 5.414756e+02 4.142917e-01
# 17 fundHICL 2015-01-02 2017-01-20 6.907755 2.718282e+00 1.639176e+05 3.935116e-01
# 18 fundMNCL 2015-01-02 2017-01-20 6.907755 7.550813e+00 7.034814e+02 1.093092e+00
# 19 fundLOCL 2015-01-02 2017-01-20 6.907755 2.718282e+00 1.359407e+08 3.935116e-01
# 20 fundCLCL 2015-01-02 2017-01-20 6.907755 2.718282e+00 5.350000e+02 3.935116e-01
2.1.6 Return of Investment
Profit and Loss
## Profit and Loss.
#'@ cbind(
#'@ OP = df.op %>% dplyr::select(Return.Back, Return.Lay) %>% colSums %>% data.frame,
#'@ HI = df.hi %>% dplyr::select(Return.Back, Return.Lay) %>% colSums %>% data.frame,
#'@ MN = df.mn %>% dplyr::select(Return.Back, Return.Lay) %>% colSums %>% data.frame,
#'@ LO = df.lo %>% dplyr::select(Return.Back, Return.Lay) %>% colSums %>% data.frame,
#'@ CL = df.cl %>% dplyr::select(Return.Back, Return.Lay) %>% colSums %>% data.frame)
## Placed orders - Fund size with log
fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
## # A tibble: 20 亊 7
## .id StartDate LatestDate InitFund LatestFund Profit RR
## <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
## 1 fundOPHI 2015-01-02 2017-01-20 1000 1326.837 326.83685 1.326837
## 2 fundHIHI 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
## 3 fundMNHI 2015-01-02 2017-01-20 1000 1152.302 152.30210 1.152302
## 4 fundLOHI 2015-01-02 2017-01-20 1000 1816.638 816.63808 1.816638
## 5 fundCLHI 2015-01-02 2017-01-20 1000 1323.186 323.18564 1.323186
## 6 fundOPMN 2015-01-02 2017-01-20 1000 1246.680 246.68001 1.246680
## 7 fundHIMN 2015-01-02 2017-01-20 1000 1384.909 384.90915 1.384909
## 8 fundMNMN 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
## 9 fundLOMN 2015-01-02 2017-01-20 1000 1529.342 529.34170 1.529342
## 10 fundCLMN 2015-01-02 2017-01-20 1000 1221.039 221.03926 1.221039
## 11 fundOPLO 2015-01-02 2017-01-20 1000 1268.312 268.31155 1.268312
## 12 fundHILO 2015-01-02 2017-01-20 1000 1649.351 649.35074 1.649351
## 13 fundMNLO 2015-01-02 2017-01-20 1000 1298.285 298.28509 1.298285
## 14 fundLOLO 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
## 15 fundCLLO 2015-01-02 2017-01-20 1000 1208.857 208.85690 1.208857
## 16 fundOPCL 2015-01-02 2017-01-20 1000 1030.560 30.55969 1.030560
## 17 fundHICL 2015-01-02 2017-01-20 1000 1400.591 400.59057 1.400591
## 18 fundMNCL 2015-01-02 2017-01-20 1000 1117.968 117.96808 1.117968
## 19 fundLOCL 2015-01-02 2017-01-20 1000 1530.690 530.68975 1.530690
## 20 fundCLCL 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
## A tibble: 20 × 7
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
# 1 fundOPHI 2015-01-02 2017-01-20 1000 2.096972e+27 7.701648e+03 2.096972e+24
# 2 fundHIHI 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 3 fundMNHI 2015-01-02 2017-01-20 1000 3.900327e+04 1.064366e+03 3.900327e+01
# 4 fundLOHI 2015-01-02 2017-01-20 1000 2.718282e+00 9.620574e+09 2.718282e-03
# 5 fundCLHI 2015-01-02 2017-01-20 1000 6.689430e+23 6.611671e+03 6.689430e+20
# 6 fundOPMN 2015-01-02 2017-01-20 1000 2.454335e+01 1.027528e+03 2.454335e-02
# 7 fundHIMN 2015-01-02 2017-01-20 1000 2.718282e+00 2.628152e+03 2.718282e-03
# 8 fundMNMN 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 9 fundLOMN 2015-01-02 2017-01-20 1000 2.718282e+00 3.916608e+04 2.718282e-03
# 10 fundCLMN 2015-01-02 2017-01-20 1000 1.585667e+01 9.900735e+02 1.585667e-02
# 11 fundOPLO 2015-01-02 2017-01-20 1000 1.152381e+00 3.455387e+03 1.152381e-03
# 12 fundHILO 2015-01-02 2017-01-20 1000 2.718282e+00 1.541190e+06 2.718282e-03
# 13 fundMNLO 2015-01-02 2017-01-20 1000 1.040560e+00 3.434093e+03 1.040560e-03
# 14 fundLOLO 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
# 15 fundCLLO 2015-01-02 2017-01-20 1000 1.152544e+00 2.646148e+03 1.152544e-03
# 16 fundOPCL 2015-01-02 2017-01-20 1000 2.919029e+00 5.465890e+02 2.919029e-03
# 17 fundHICL 2015-01-02 2017-01-20 1000 2.718282e+00 2.278748e+05 2.718282e-03
# 18 fundMNCL 2015-01-02 2017-01-20 1000 1.034658e+01 9.680842e+02 1.034658e-02
# 19 fundLOCL 2015-01-02 2017-01-20 1000 2.718282e+00 2.407137e+08 2.718282e-03
# 20 fundCLCL 2015-01-02 2017-01-20 1000 2.718282e+00 5.350000e+02 2.718282e-03
llply(fundList, function(x) x[c('BR', 'fB', 'fS', 'EUB', 'EUS', 'Edge', 'PF', 'FC', 'Buy', 'Sell', 'BuyS', 'SellS', 'Profit', 'Bal', 'RR')] %>% filter(PF >0 | FC > 0))
## $fundOPHI
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.007746509 -0.007746509 6.907785 6.907785 6.907785 0.0000
## 2 1000.000 -0.110082616 0.110082616 6.913827 6.913827 6.913827 119.8876
## 3 1007.068 -0.039258154 0.039258154 6.915569 6.915569 6.915569 0.0000
## 4 1007.068 0.003366522 -0.003366522 6.914804 6.914804 6.914804 119.4291
## 5 1007.283 -0.098121256 0.098121256 6.919834 6.919834 6.919834 0.0000
## 6 1007.283 -0.067667134 0.067667134 6.917303 6.917303 6.917303 119.3111
## 7 1011.621 -0.044935421 0.044935421 6.920319 6.920319 6.920319 0.0000
## 8 1011.621 -0.103542601 0.103542601 6.924679 6.924679 6.924679 118.2559
## 9 1018.277 -0.048376831 0.048376831 6.927038 6.927038 6.927038 0.0000
## 10 1018.277 0.011428721 -0.011428721 6.925932 6.925932 6.925932 117.7855
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundHIHI
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF FC Buy Sell
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000 0 0 6.907755 6.907755 0 0.0000 119.8700 0 0
## 2 1000 0 0 6.907755 6.907755 0 0.0000 120.4340 0 0
## 3 1000 0 0 6.907755 6.907755 0 0.0000 119.4250 0 0
## 4 1000 0 0 6.907755 6.907755 0 119.3980 119.3980 0 0
## 5 1000 0 0 6.907755 6.907755 0 119.5863 119.5863 0 0
## 6 1000 0 0 6.907755 6.907755 0 0.0000 119.7920 0 0
## 7 1000 0 0 6.907755 6.907755 0 0.0000 118.2120 0 0
## 8 1000 0 0 6.907755 6.907755 0 0.0000 118.3280 0 0
## 9 1000 0 0 6.907755 6.907755 0 0.0000 117.7950 0 0
## 10 1000 0 0 6.907755 6.907755 0 117.6797 117.6797 0 0
## # ... with 525 more rows, and 5 more variables: BuyS <dbl>, SellS <dbl>,
## # Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundMNHI
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.0000 0.003821349 -0.003821349 6.907763 6.907763 6.907763 0.0000
## 2 1000.0000 -0.047573272 0.047573272 6.908887 6.908887 6.908887 120.4692
## 3 996.9558 -0.079100269 0.079100269 6.907838 6.907838 6.907838 0.0000
## 4 996.9558 -0.051211075 0.051211075 6.906018 6.906018 6.906018 118.9236
## 5 1000.2317 -0.056007009 0.056007009 6.909556 6.909556 6.909556 0.0000
## 6 1000.2317 -0.038230691 0.038230691 6.908718 6.908718 6.908718 119.5842
## 7 1002.6775 -0.102753826 0.102753826 6.915718 6.915718 6.915718 119.2241
## 8 996.0814 -0.070077510 0.070077510 6.906286 6.906286 6.906286 118.5676
## 9 991.5957 -0.065126763 0.065126763 6.901438 6.901438 6.901438 0.0000
## 10 991.5957 -0.113308820 0.113308820 6.905749 6.905749 6.905749 116.6273
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundLOHI
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.02343199 -0.02343199 6.908030 6.908030 6.908030 0.0000
## 2 1000.000 -0.11560682 0.11560682 6.914453 6.914453 6.914453 119.8360
## 3 1007.425 -0.12995507 0.12995507 6.923621 6.923621 6.923621 0.0000
## 4 1007.425 -0.07743216 0.07743216 6.918154 6.918154 6.918154 118.6801
## 5 1012.392 -0.09830446 0.09830446 6.924911 6.924911 6.924911 0.0000
## 6 1012.392 -0.08069380 0.08069380 6.923330 6.923330 6.923330 119.1899
## 7 1017.572 -0.15022346 0.15022346 6.936501 6.936501 6.936501 118.7790
## 8 1007.869 -0.11925540 0.11925540 6.922721 6.922721 6.922721 118.1091
## 9 1015.539 -0.10678380 0.10678380 6.928887 6.928887 6.928887 117.7410
## 10 1022.409 -0.16977067 0.16977067 6.944398 6.944398 6.944398 0.0000
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundCLHI
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.007628568 -0.007628568 6.907784 6.907784 6.907784 0.0000
## 2 1000.000 -0.112305972 0.112305972 6.914075 6.914075 6.914075 119.8669
## 3 1007.212 -0.034748669 0.034748669 6.915545 6.915545 6.915545 0.0000
## 4 1007.212 0.004968436 -0.004968436 6.914953 6.914953 6.914953 119.4439
## 5 1007.530 -0.097873082 0.097873082 6.920054 6.920054 6.920054 0.0000
## 6 1007.530 -0.070867097 0.070867097 6.917770 6.917770 6.917770 119.2813
## 7 1012.073 -0.042185896 0.042185896 6.920647 6.920647 6.920647 0.0000
## 8 1012.073 -0.103713569 0.103713569 6.925144 6.925144 6.925144 118.2544
## 9 1018.741 -0.043888865 0.043888865 6.927286 6.927286 6.927286 0.0000
## 10 1018.741 0.013426699 -0.013426699 6.926413 6.926413 6.926413 117.8040
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundOPMN
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.003908171 -0.003908171 6.907763 6.907763 6.907763 0.0000
## 2 1000.000 -0.062495264 0.062495264 6.909709 6.909709 6.909709 119.8876
## 3 1004.019 0.039765939 -0.039765939 6.912557 6.912557 6.912557 0.0000
## 4 1004.019 0.054333121 -0.054333121 6.913243 6.913243 6.913243 119.4291
## 5 1007.514 -0.042113105 0.042113105 6.916128 6.916128 6.916128 0.0000
## 6 1007.514 -0.029368111 0.029368111 6.915672 6.915672 6.915672 119.3111
## 7 1009.402 0.057777002 -0.057777002 6.918784 6.918784 6.918784 0.0000
## 8 1009.402 -0.033510743 0.033510743 6.917675 6.917675 6.917675 118.2559
## 9 1011.558 0.016718446 -0.016718446 6.919387 6.919387 6.919387 0.0000
## 10 1011.558 0.124072746 -0.124072746 6.926964 6.926964 6.926964 117.7855
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundHIMN
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.003804721 0.003804721 6.907763 6.907763 6.907763 0.0000
## 2 1000.000 0.047366510 -0.047366510 6.908877 6.908877 6.908877 0.0000
## 3 1000.000 0.078757201 -0.078757201 6.910860 6.910860 6.910860 0.0000
## 4 1000.000 0.050988544 -0.050988544 6.909056 6.909056 6.909056 119.3980
## 5 1003.277 0.055763702 -0.055763702 6.912583 6.912583 6.912583 119.5863
## 6 1006.864 0.038064463 -0.038064463 6.915320 6.915320 6.915320 0.0000
## 7 1006.864 0.102309175 -0.102309175 6.919839 6.919839 6.919839 0.0000
## 8 1006.864 0.069773361 -0.069773361 6.917032 6.917032 6.917032 0.0000
## 9 1006.864 0.064844001 -0.064844001 6.916700 6.916700 6.916700 0.0000
## 10 1006.864 0.112819083 -0.112819083 6.920973 6.920973 6.920973 117.6797
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundMNMN
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF FC Buy Sell
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000 0 0 6.907755 6.907755 0 0.0000 119.8700 0 0
## 2 1000 0 0 6.907755 6.907755 0 120.4692 120.4692 0 0
## 3 1000 0 0 6.907755 6.907755 0 0.0000 119.4250 0 0
## 4 1000 0 0 6.907755 6.907755 0 118.9236 118.9236 0 0
## 5 1000 0 0 6.907755 6.907755 0 0.0000 119.2890 0 0
## 6 1000 0 0 6.907755 6.907755 0 119.5842 119.5842 0 0
## 7 1000 0 0 6.907755 6.907755 0 119.2241 119.2241 0 0
## 8 1000 0 0 6.907755 6.907755 0 118.5676 118.5676 0 0
## 9 1000 0 0 6.907755 6.907755 0 0.0000 117.7950 0 0
## 10 1000 0 0 6.907755 6.907755 0 116.6273 116.6273 0 0
## # ... with 525 more rows, and 5 more variables: BuyS <dbl>, SellS <dbl>,
## # Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundLOMN
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.01952670 -0.01952670 6.907946 6.907946 6.907946 0.0000
## 2 1000.000 -0.06803250 0.06803250 6.910071 6.910071 6.910071 119.8360
## 3 1004.376 -0.05104664 0.05104664 6.913425 6.913425 6.913425 0.0000
## 4 1004.376 -0.02618864 0.02618864 6.912465 6.912465 6.912465 118.6801
## 5 1006.059 -0.04229664 0.04229664 6.914691 6.914691 6.914691 0.0000
## 6 1006.059 -0.04238127 0.04238127 6.914695 6.914695 6.914695 119.1899
## 7 1008.785 -0.04784473 0.04784473 6.917647 6.917647 6.917647 118.7790
## 8 1011.864 -0.04928814 0.04928814 6.920764 6.920764 6.920764 118.1091
## 9 1015.037 -0.04170399 0.04170399 6.923550 6.923550 6.923550 117.7410
## 10 1017.723 -0.05708155 0.05708155 6.926953 6.926953 6.926953 0.0000
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundCLMN
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.003790739 -0.003790739 6.907762 6.907762 6.907762 0.0000
## 2 1000.000 -0.064723580 0.064723580 6.909851 6.909851 6.909851 119.8669
## 3 1004.162 0.044254405 -0.044254405 6.912889 6.912889 6.912889 0.0000
## 4 1004.162 0.055924278 -0.055924278 6.913474 6.913474 6.913474 119.4439
## 5 1007.760 -0.041864475 0.041864475 6.916362 6.916362 6.916362 0.0000
## 6 1007.760 -0.032563800 0.032563800 6.916016 6.916016 6.916016 119.2813
## 7 1009.854 0.060511114 -0.060511114 6.919393 6.919393 6.919393 0.0000
## 8 1009.854 -0.033682265 0.033682265 6.918129 6.918129 6.918129 118.2544
## 9 1012.021 0.021193104 -0.021193104 6.919929 6.919929 6.919929 0.0000
## 10 1012.021 0.126037785 -0.126037785 6.927669 6.927669 6.927669 117.8040
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundOPLO
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.0154694206 0.0154694206 6.907875 6.907875 6.907875
## 2 1000.000 0.0055024196 -0.0055024196 6.907770 6.907770 6.907770
## 3 1000.357 0.0898005337 -0.0898005337 6.912150 6.912150 6.912150
## 4 1000.357 0.0796616632 -0.0796616632 6.911288 6.911288 6.911288
## 5 1005.534 0.0001820308 -0.0001820308 6.913274 6.913274 6.913274
## 6 1005.534 0.0129007562 -0.0129007562 6.913357 6.913357 6.913357
## 7 1006.371 0.1043859330 -0.1043859330 6.919564 6.919564 6.919564
## 8 1006.371 0.0156461385 -0.0156461385 6.914228 6.914228 6.914228
## 9 1007.386 0.0578304554 -0.0578304554 6.916788 6.916788 6.916788
## 10 1007.386 0.1784473815 -0.1784473815 6.931122 6.931122 6.931122
## # ... with 525 more rows, and 9 more variables: PF <dbl>, FC <dbl>,
## # Buy <dbl>, Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>,
## # Bal <dbl>, RR <dbl>
##
## $fundHILO
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.02310598 0.02310598 6.908022 6.908022 6.908022 0.0000
## 2 1000.000 0.11400900 -0.11400900 6.914268 6.914268 6.914268 0.0000
## 3 1000.000 0.12816224 -0.12816224 6.915991 6.915991 6.915991 0.0000
## 4 1000.000 0.07635786 -0.07635786 6.910673 6.910673 6.910673 119.3980
## 5 1004.961 0.09694316 -0.09694316 6.917410 6.917410 6.917410 119.5863
## 6 1011.272 0.07957455 -0.07957455 6.922133 6.922133 6.922133 0.0000
## 7 1011.272 0.14815718 -0.14815718 6.929980 6.929980 6.929980 0.0000
## 8 1011.272 0.11760788 -0.11760788 6.925896 6.925896 6.925896 0.0000
## 9 1011.272 0.10530642 -0.10530642 6.924519 6.924519 6.924519 0.0000
## 10 1011.272 0.16744322 -0.16744322 6.933049 6.933049 6.933049 117.6797
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundMNLO
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.01933914 0.01933914 6.907942 6.907942 6.907942 0.0000
## 2 1000.000 0.06738044 -0.06738044 6.910027 6.910027 6.910027 120.4692
## 3 1004.376 0.05055688 -0.05055688 6.913400 6.913400 6.913400 0.0000
## 4 1004.376 0.02593713 -0.02593713 6.912458 6.912458 6.912458 118.9236
## 5 1006.059 0.04189066 -0.04189066 6.914674 6.914674 6.914674 0.0000
## 6 1006.059 0.04197447 -0.04197447 6.914677 6.914677 6.914677 119.5842
## 7 1008.785 0.04738561 -0.04738561 6.917625 6.917625 6.917625 119.2241
## 8 1011.864 0.04881520 -0.04881520 6.920741 6.920741 6.920741 118.5676
## 9 1015.037 0.04130368 -0.04130368 6.923534 6.923534 6.923534 0.0000
## 10 1015.037 0.05653407 -0.05653407 6.924279 6.924279 6.924279 116.6273
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundLOLO
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF FC Buy Sell
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000 0 0 6.907755 6.907755 0 0.0000 119.8700 0 0
## 2 1000 0 0 6.907755 6.907755 0 119.8360 119.8360 0 0
## 3 1000 0 0 6.907755 6.907755 0 0.0000 119.4250 0 0
## 4 1000 0 0 6.907755 6.907755 0 118.6801 118.6801 0 0
## 5 1000 0 0 6.907755 6.907755 0 0.0000 119.2890 0 0
## 6 1000 0 0 6.907755 6.907755 0 119.1899 119.1899 0 0
## 7 1000 0 0 6.907755 6.907755 0 118.7790 118.7790 0 0
## 8 1000 0 0 6.907755 6.907755 0 118.1091 118.1091 0 0
## 9 1000 0 0 6.907755 6.907755 0 117.7410 117.7410 0 0
## 10 1000 0 0 6.907755 6.907755 0 0.0000 117.3900 0 0
## # ... with 525 more rows, and 5 more variables: BuyS <dbl>, SellS <dbl>,
## # Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundCLLO
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.0155857038 0.0155857038 6.907877 6.907877 6.907877
## 2 1000.000 0.0032885057 -0.0032885057 6.907761 6.907761 6.907761
## 3 1000.213 0.0942224169 -0.0942224169 6.912414 6.912414 6.912414
## 4 1000.213 0.0812332685 -0.0812332685 6.911272 6.911272 6.911272
## 5 1005.493 0.0004286137 -0.0004286137 6.913233 6.913233 6.913233
## 6 1005.493 0.0097337017 -0.0097337017 6.913280 6.913280 6.913280
## 7 1006.124 0.1070773642 -0.1070773642 6.919605 6.919605 6.919605
## 8 1006.124 0.0154761460 -0.0154761460 6.913981 6.913981 6.913981
## 9 1007.129 0.0622508181 -0.0622508181 6.916798 6.916798 6.916798
## 10 1007.129 0.1803677688 -0.1803677688 6.931214 6.931214 6.931214
## # ... with 525 more rows, and 9 more variables: PF <dbl>, FC <dbl>,
## # Buy <dbl>, Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>,
## # Bal <dbl>, RR <dbl>
##
## $fundOPCL
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 0.0001171625 -0.0001171625 6.907755 6.907755 6.907755
## 2 1000.000 0.0022302643 -0.0022302643 6.907758 6.907758 6.907758
## 3 1000.144 -0.0044843098 0.0044843098 6.907909 6.907909 6.907909
## 4 1000.144 -0.0015912841 0.0015912841 6.907900 6.907900 6.907900
## 5 1000.246 -0.0002484005 0.0002484005 6.908001 6.908001 6.908001
## 6 1000.246 0.0031907134 -0.0031907134 6.908006 6.908006 6.908006
## 7 1000.451 -0.0027353174 0.0027353174 6.908210 6.908210 6.908210
## 8 1000.451 0.0001712781 -0.0001712781 6.908207 6.908207 6.908207
## 9 1000.462 -0.0044655769 0.0044655769 6.908228 6.908228 6.908228
## 10 1000.462 -0.0019849334 0.0019849334 6.908220 6.908220 6.908220
## # ... with 525 more rows, and 9 more variables: PF <dbl>, FC <dbl>,
## # Buy <dbl>, Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>,
## # Bal <dbl>, RR <dbl>
##
## $fundHICL
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.007577851 0.007577851 6.907784 6.907784 6.907784 0.0000
## 2 1000.000 0.111564204 -0.111564204 6.913992 6.913992 6.913992 0.0000
## 3 1000.000 0.034517786 -0.034517786 6.908351 6.908351 6.908351 0.0000
## 4 1000.000 -0.004935404 0.004935404 6.907767 6.907767 6.907767 119.3980
## 5 1000.318 0.097225611 -0.097225611 6.912807 6.912807 6.912807 119.5863
## 6 1006.596 0.070397164 -0.070397164 6.916810 6.916810 6.916810 0.0000
## 7 1006.596 0.041905681 -0.041905681 6.915208 6.915208 6.915208 0.0000
## 8 1006.596 0.103027885 -0.103027885 6.919647 6.919647 6.919647 0.0000
## 9 1006.596 0.043597360 -0.043597360 6.915281 6.915281 6.915281 0.0000
## 10 1006.596 -0.013337439 0.013337439 6.914419 6.914419 6.914419 117.6797
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundMNCL
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.000 -0.003781993 0.003781993 6.907762 6.907762 6.907762 0.0000
## 2 1000.000 0.064574574 -0.064574574 6.909842 6.909842 6.909842 120.4692
## 3 1004.162 -0.044152403 0.044152403 6.912884 6.912884 6.912884 0.0000
## 4 1004.162 -0.055795457 0.055795457 6.913466 6.913466 6.913466 118.9236
## 5 1007.760 0.041767972 -0.041767972 6.916358 6.916358 6.916358 0.0000
## 6 1007.760 0.032488709 -0.032488709 6.916013 6.916013 6.916013 119.5842
## 7 1009.854 -0.060371767 0.060371767 6.919385 6.919385 6.919385 119.2241
## 8 1005.958 0.033604597 -0.033604597 6.914260 6.914260 6.914260 118.5676
## 9 1008.124 -0.021144218 0.021144218 6.916070 6.916070 6.916070 0.0000
## 10 1008.124 -0.125749407 0.125749407 6.923774 6.923774 6.923774 116.6273
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundLOCL
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000.0000 0.015700571 -0.015700571 6.907879 6.907879 6.907879 0.0000
## 2 1000.0000 -0.003312745 0.003312745 6.907761 6.907761 6.907761 119.8360
## 3 1000.2132 -0.094913656 0.094913656 6.912480 6.912480 6.912480 0.0000
## 4 1000.2132 -0.081829942 0.081829942 6.911320 6.911320 6.911320 118.6801
## 5 1005.4926 -0.000431773 0.000431773 6.913233 6.913233 6.913233 0.0000
## 6 1005.4926 -0.009805445 0.009805445 6.913281 6.913281 6.913281 119.1899
## 7 1006.1243 -0.107861822 0.107861822 6.919689 6.919689 6.919689 118.7790
## 8 999.1479 -0.015590206 0.015590206 6.907024 6.907024 6.907024 118.1091
## 9 1000.1514 -0.062708726 0.062708726 6.909874 6.909874 6.909874 117.7410
## 10 1004.1934 -0.181674125 0.181674125 6.928535 6.928535 6.928535 0.0000
## # ... with 525 more rows, and 8 more variables: FC <dbl>, Buy <dbl>,
## # Sell <dbl>, BuyS <dbl>, SellS <dbl>, Profit <dbl>, Bal <dbl>, RR <dbl>
##
## $fundCLCL
## # A tibble: 535 亊 15
## BR fB fS EUB EUS Edge PF FC Buy Sell
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000 0 0 6.907755 6.907755 0 0.0000 119.8700 0 0
## 2 1000 0 0 6.907755 6.907755 0 119.8669 119.8669 0 0
## 3 1000 0 0 6.907755 6.907755 0 0.0000 119.4250 0 0
## 4 1000 0 0 6.907755 6.907755 0 119.4439 119.4439 0 0
## 5 1000 0 0 6.907755 6.907755 0 0.0000 119.2890 0 0
## 6 1000 0 0 6.907755 6.907755 0 119.2813 119.2813 0 0
## 7 1000 0 0 6.907755 6.907755 0 0.0000 118.2120 0 0
## 8 1000 0 0 6.907755 6.907755 0 118.2544 118.2544 0 0
## 9 1000 0 0 6.907755 6.907755 0 0.0000 117.7950 0 0
## 10 1000 0 0 6.907755 6.907755 0 117.8040 117.8040 0 0
## # ... with 525 more rows, and 5 more variables: BuyS <dbl>, SellS <dbl>,
## # Profit <dbl>, Bal <dbl>, RR <dbl>
2.1.7 Return of Investment Optimization
## optimise the stakes.
#'@ log(balance + portion Kelly edge ratio)
2.2 Question 2
When I .